//
//  KAVCManager.swift
//  kano
//
//  Created by Sophia on 2024/8/14.
//

import UIKit

final class KAVCManager: NSObject {
    //Singleton
    static var shared = KAVCManager()
    
    private override init() {
        super.init()
    }
    
    var tabbarVC: KATabVC?
    
    static func setRooterVC() {
        KAVCManager.shared.destroyRootVC()
        if KAUserManager.shared.hasLogined {
            //进入首页
            let rootVC = KAVCManager.shared.createRootVC()
            UIApplication.shared.delegate?.window??.rootViewController = rootVC
        }
        else {
            //用userGid判断是防止修改手机号后用手机号判断有问题
            if KAUserManager.shared.user.userGid.isEmpty {
                //登录页
                let loginNav = KANavigationVC(rootViewController: KALoginVC())
                UIApplication.shared.delegate?.window??.rootViewController = loginNav
            }
            else {
                //输入密码页
                let loginNav = KANavigationVC(rootViewController: KAInputPwdVC())
                UIApplication.shared.delegate?.window??.rootViewController = loginNav
            }
        }
    }
    
    //切换到输入登录手机号页
    static func switchRootVC(_ needSwitch: Bool) {
        let vc = KALoginVC()
        vc.var_mobileView.contentTF.text = needSwitch ? "" : KAUserManager.shared.user.mobile
        let loginNav = KANavigationVC(rootViewController: vc)
        UIApplication.shared.delegate?.window??.rootViewController = loginNav
    }
    
    //创建tabbar
    func createRootVC() -> UIViewController? {
        self.tabbarVC = KATabVC()
        self.tabbarVC?.ka_addChildVC("Loan", "tab_home_unsel", "tab_home_sel", KAHomeVC.self)
        self.tabbarVC?.ka_addChildVC("Mine", "tab_mine_unsel", "tab_mine_sel", KAMineVC.self)
        return self.tabbarVC
    }
    
    func destroyRootVC() {
        KAVCManager.shared.tabbarVC = nil
    }
}

